/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.integration.cae.util;

/**
 *
 * @author Jose
 */
public class UtilsString {

    public static int LEFT = 0;
    public static int RIGHT = 1;

    public static String fill(String src, int max_size, char fill_char, int side) {
        String toRet = src;
        if (src == null) {
            toRet = "";
        }
        if (toRet.length() >= max_size) {
            return toRet.substring(0, max_size);
        }
        int total_size = toRet.length();
        for (int n = 0; n < max_size - total_size; n++) {
            if (side == LEFT) {
                toRet = fill_char + toRet;
                continue;
            }
            if (side == RIGHT) {
                toRet = toRet + fill_char;
            }
        }

        return toRet;
    }

    public static String[] parseLine(String line, byte type, String comp_type)
            throws Exception {
        String data[] = null;
        if (type == 0) {
            Tokenizer fst = new Tokenizer(line, comp_type);
            data = new String[fst.countTokens()];
            for (int n = 0; n < data.length; n++) {
                data[n] = fst.nextToken();
            }

        } else if (type == 1) {
            Tokenizer stk = new Tokenizer(comp_type, ",");
            int sizes[] = new int[stk.countTokens()];
            for (int n = 0; n < sizes.length; n++) {
                sizes[n] = Integer.parseInt(stk.nextToken());
            }

            data = splitString(line, sizes);
        } else {
            throw new Exception("Unsupported type of file [" + type + "]");
        }
        return data;
    }

    public static synchronized String[] splitString(String string, int sizes[]) {
        int actual = 0;
        String returnString[] = new String[sizes.length];
        for (int n = 0; n < sizes.length; n++) {
            returnString[n] = string.substring(actual, actual + sizes[n]);
            actual += sizes[n];
        }

        return returnString;
    }
}
